//
//  YDUserAccountViewModel.swift
//  YDWeibo01
//
//  Created by suibian on 16/8/11.
//  Copyright © 2016年 iitcast. All rights reserved.
//

import UIKit

class YDUserAccountViewModel: NSObject {
//    全局访问点
    static let sharedTools:YDUserAccountViewModel = YDUserAccountViewModel()
    
//    创建用户信息对象(为了防止频繁的操作沙盒)
    var useAccount:YDUserAccountModel?
    override init() {
        super.init()
//        给当前类身上的属性赋值
        useAccount = getUserAccount()
    }
    //  抽取路径
    let file = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
//    accesstoken
    var access_token:String?{
        if useAccount?.access_token != nil{
            if useAccount?.expire_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
            return useAccount?.access_token
            }else{
            
            return nil
            }
        
        
        }else{
        
        return nil
        }
        
    
    }
//    判断用户是否登录
    var isLogin:Bool{
    
    return access_token != nil
    }
    
}

//保存数据
extension YDUserAccountViewModel{

//保存对象
    func saveUserAccount(userAccount:YDUserAccountModel){
//        防止单例创建的时候 没有赋值的机会  刚开始没有保存到沙盒 获取到数据为nil (可选值) 所以在保存对象的时候
        self.useAccount = userAccount
     
    NSKeyedArchiver.archiveRootObject(userAccount, toFile: file)
        print(file)
        
    
    }
//    获得对象 可能返回为nil 所以加?
    
    func getUserAccount()->YDUserAccountModel?{
//    获取路径
      
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? YDUserAccountModel
        return result
    
    }

}
//请求数据
    extension YDUserAccountViewModel{
        func getUseCount(code:String,finish:(issuccess:Bool) -> ()){
            YDNetWorkTools.shareTools.oauthLoadUsetAcount(code, success: { (response) -> () in
                guard let res = response as? [String:AnyObject] else{
//                    
                    finish(issuccess: false)
                    return
                }
                //            通过字典创建模型
                let userAccount = YDUserAccountModel(dict:res)
                print(userAccount)
                self.getUserInfo(userAccount,finish: finish)
                }) { (error) -> () in
                    print("请求失败\(error)")
            }
        }
        
        //    请求个人信息
        func getUserInfo(userAccount:YDUserAccountModel,finish:(issuccess:Bool) -> ()){
            YDNetWorkTools.shareTools.oauthLoadUserAccount(userAccount, success: { (response) -> () in
                guard let res = response as?[String:AnyObject] else{
                    finish(issuccess: false)
                    return
                }
                //            赋值 赋值完后保存对象
                
                userAccount.avatar_large = res["avatar_large"] as?String
                userAccount.screen_name = res["screen_name"] as?String
//                保存对像
                self.saveUserAccount(userAccount)
                
                finish(issuccess: true)
                }) { (error) -> () in
                    print("请求失败\(error)")
                    finish(issuccess: false)
            }
        }

        

    }
    

